package org.activision.model.player;

import java.io.Serializable;

import org.activision.model.Container;
import org.activision.model.Item;
import org.activision.util.rs2cache.ItemDefinitions;

public class Equipment implements Serializable{
	
	private static final long serialVersionUID = -2934091862414178786L;

	private transient Player player;
	
	public static final byte SLOT_HAT = 0, SLOT_CAPE = 1, SLOT_AMULET = 2, SLOT_WEAPON = 3, SLOT_CHEST = 4, SLOT_SHIELD = 5, SLOT_LEGS = 7, SLOT_HANDS = 9, SLOT_FEET = 10, SLOT_RING = 12, SLOT_ARROWS = 13;
	
	private Container<Item> equipment = new Container<Item>(15, false);
	
	public Container<Item> getEquipment() {
		return equipment;
	}
	
	public void setPlayer(Player player) {
    	for(Item item : getEquipment().getItems())
        	if(item != null)
            item.setDefinition(ItemDefinitions.forID(item.getId()));
		this.player = player;
	}
	
	public boolean contains(int item) {
		return equipment.containsOne(new Item(item));
	}
	public void deleteItem(int item, int amount) {
		equipment.remove(new Item(item, amount));
		refresh();
	}
	
	public Item get(int slot) {
		return equipment.get(slot);
	}
	
	public void set(int slot, Item item) {
		equipment.set(slot, item);
		//if (slot == 3)
			//reset spec
		refresh();
	}
	
	public void clear() {
		equipment.clear();
		refresh();
	}
	
	public void refresh() {
		//if (player.isOnline())
		//player.getActionSender().SendSound(2248,100,0);
		player.getMask().setApperanceUpdate(true);
		player.getFrames().sendItems(94, equipment, false);
		player.getCombatDefinitions().refreshBonuses();
		player.getCombat().removeTarget();
		player.getCombatDefinitions().setSpecialOff();
	}
	
	private static String[] CAPES = {"druidic cloak", "cape","Cape","Ava's accumulator"};
	private static String[] HATS = {"druidic wreath", "halo", "Royal", "crown", "sallet","helm","hood","coif","Coif","hat","partyhat","Hat","full helm (t)","full helm (g)","hat (t)","hat (g)","cav","boater","helmet","mask","Helm of neitiznot"};
	private static String[] BOOTS = {"boots","Boots", "shoes", "Shoes"};
	private static String[] GLOVES = {"gloves","gauntlets","Gloves","vambraces","vamb","bracers"};
	private static String[] SHIELDS = {"kiteshield","sq shield","Toktz-ket","books","book","kiteshield (t)","kiteshield (g)","kiteshield(h)","defender","shield"};
	private static String[] AMULETS = {"amulet","necklace","Amulet of", "scarf"};
	private static String[] ARROWS = {"arrow","arrows","arrow(p)","arrow(+)","arrow(s)","bolt","Bolt rack","Opal bolts","Dragon bolts", "bolts (e)", "bolts"};
	private static String[] RINGS = {"ring","Ring"};
	private static String[] BODY = {"armour", "hauberk","platebody","chainbody","robetop","leathertop","platemail","top","brassard","Robe top","body","platebody (t)","platebody (g)","body(g)","body_(g)","chestplate","torso","shirt"};
	private static String[] LEGS = {"druidic robe", "cuisse", "pants","platelegs","plateskirt","skirt","bottoms","chaps","platelegs (t)","platelegs (g)","bottom","skirt","skirt (g)","skirt (t)","chaps (g)","chaps (t)","tassets","legs", "trousers", "robe bottom"};
	private static String[] WEAPONS = {"bow", "Hand cannon","Inferno adze", "Silverlight","Darklight","wand", "Statius's warhammer", "anchor","spear.", "Vesta's longsword.", "scimitar","longsword","sword","longbow","shortbow","dagger","mace","halberd","spear",
	"Abyssal whip","axe","flail","crossbow","Torags hammers","dagger(p)","dagger(+)","dagger(s)","spear(p)","spear(+)",
	"spear(s)","spear(kp)","maul","dart","dart(p)","javelin","javelin(p)","knife","knife(p)","Longbow","Shortbow",
	"Crossbow","Toktz-xil","Toktz-mej","Tzhaar-ket","staff","Staff","godsword","c'bow","Crystal bow","Dark bow","claws", "warhammer", "adze", "hand"};
	private static String[] FULL_BODY = {"armour", "hauberk", "top","shirt","platebody","Ahrims robetop","Karils leathertop","brassard","Robe top","robetop","platebody (t)","platebody (g)","chestplate","torso", "Morrigan's", "leather body", "chainbody", "Zuriel's", "robe top"};
	private static String[] FULL_HAT = {"sallet", "med helm","coif","Dharoks helm","hood","Initiate helm","Coif","Helm of neitiznot"};
	private static String[] FULL_MASK = {"sallet","full helm","mask","Veracs helm","Guthans helm","Torags helm","Karils coif","full helm (t)","full helm (g)","mask"};
	
	public static int getItemType(int wearId) {
		try {
		String weapon = ItemDefinitions.forID(wearId).name;
		for(int i = 0; i < CAPES.length; i++) {
			if(weapon.contains(CAPES[i]))
				return 1;
		}
		for(int i = 0; i < HATS.length; i++) {
			if(weapon.contains(HATS[i]))
				return 0;
		}
		for(int i = 0; i < BOOTS.length; i++) {
			if(weapon.endsWith(BOOTS[i]) || weapon.startsWith(BOOTS[i]))
				return 10;
		}
		for(int i = 0; i < GLOVES.length; i++) {
			if(weapon.endsWith(GLOVES[i]) || weapon.startsWith(GLOVES[i]))
				return 9;
		}
		for(int i = 0; i < SHIELDS.length; i++) {
			if(weapon.contains(SHIELDS[i]))
				return 5;
		}
		for(int i = 0; i < AMULETS.length; i++) {
			if(weapon.endsWith(AMULETS[i]) || weapon.startsWith(AMULETS[i]))
				return 2;
		}
		for(int i = 0; i < ARROWS.length; i++) {
			if(weapon.endsWith(ARROWS[i]) || weapon.startsWith(ARROWS[i]))
				return 13;
		}
		for(int i = 0; i < RINGS.length; i++) {
			if(weapon.endsWith(RINGS[i]) || weapon.startsWith(RINGS[i]))
				return 12;
		}
		for(int i = 0; i < BODY.length; i++) {
			if(weapon.contains(BODY[i]))
				return 4;
		}
		for(int i = 0; i < LEGS.length; i++) {
			if(weapon.contains(LEGS[i]))
				return 7;
		}
		for(int i = 0; i < WEAPONS.length; i++) {
			if(weapon.endsWith(WEAPONS[i]) || weapon.startsWith(WEAPONS[i]))
				return 3;
		}
		return -1;
		} catch(Exception e) {
		return -1;
		}
	}
	
	public static boolean isFullBody(ItemDefinitions def) {
		String weapon = def.name;
		for (int i = 0; i < FULL_BODY.length; i++) {
			if (weapon.contains(FULL_BODY[i])) {
				return true;
			}
		}
		return false;
	}

 
	public static boolean isFullHat(ItemDefinitions def) {
		String weapon = def.name;
		for (int i = 0; i < FULL_HAT.length; i++) {
			if (weapon.endsWith(FULL_HAT[i])) {
				return true;
			}
		}
		return false;
	}

	public static boolean isFullMask(ItemDefinitions def) {
		String weapon = def.name;
		for (int i = 0; i < FULL_MASK.length; i++) {
			if (weapon.endsWith(FULL_MASK[i])) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isTwoHanded(ItemDefinitions def) {
		String wepEquiped = def.name;
		int itemId = def.getId();
		if (itemId == 4212)
			return true;
		else if (itemId == 4214)
			return true;
		else if (wepEquiped.endsWith("claws"))
			return true;
		else if (wepEquiped.endsWith("anchor"))
			return true;
		else if (wepEquiped.endsWith("2h sword"))
			return true;
		else if (wepEquiped.endsWith("longbow"))
			return true;
		else if (wepEquiped.equals("Seercull"))
			return true;
		else if (wepEquiped.endsWith("shortbow"))
			return true;
		else if (wepEquiped.endsWith("Longbow"))
			return true;
		else if (wepEquiped.endsWith("Shortbow"))
			return true;
		else if (wepEquiped.endsWith("bow full"))
			return true;
		else if (wepEquiped.equals("Dark bow"))
			return true;
		else if (wepEquiped.endsWith("halberd"))
			return true;
		else if (wepEquiped.equals("Granite maul"))
			return true;
		else if (wepEquiped.equals("Karils crossbow"))
			return true;
		else if (wepEquiped.equals("Torags hammers"))
			return true;
		else if (wepEquiped.equals("Veracs flail"))
			return true;
		else if (wepEquiped.equals("Dharoks greataxe"))
			return true;
		else if (wepEquiped.equals("Guthans warspear"))
			return true;
		else if (wepEquiped.equals("Tzhaar-ket-om"))
			return true;
		else if (wepEquiped.endsWith("godsword"))
			return true;
		else if (wepEquiped.equals("Saradomin sword"))
			return true;
		else
			return false;
	}
	
	public int getRenderAnim() { //TODO cache sided
		if(get(3) == null)
			return 1426;
		int renderEmote = get(3).getDefinition().renderEmote;
		if(renderEmote != 0)
			return renderEmote;
		return 1426;
	}
	
}
